package LC;

import java.util.*;

public class LC239 {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(maxSlidingWindow2(new int[]{1, 2, 12, 4, 5}, 3)));
    }

    public int[] maxSlidingWindow(int[] nums, int k) {
        int n = nums.length;
        PriorityQueue<int[]> pq = new PriorityQueue<int[]>(new Comparator<int[]>() {
            public int compare(int[] pair1, int[] pair2) {
                return pair1[0] != pair2[0] ? pair2[0] - pair1[0] : pair2[1] - pair1[1];
            }
        });
        for (int i = 0; i < k; ++i) {
            pq.offer(new int[]{nums[i], i});
        }
        int[] ans = new int[n - k + 1];
        ans[0] = pq.peek()[0];
        for (int i = k; i < n; ++i) {
            pq.offer(new int[]{nums[i], i});
            while (pq.peek()[1] <= i - k) {
                pq.poll();
            }
            ans[i - k + 1] = pq.peek()[0];
        }
        return ans;
    }


    public static int[] maxSlidingWindow2(int[] nums, int k) {
        int[] ans = new int[nums.length - k + 1];
        int[] ints = new int[k];
        for (int i = 0; i <= nums.length - k; i++) {
            System.arraycopy(nums,i,ints,0,k);
            Arrays.sort(ints);
            ans[i] = ints[ints.length-1];
        }
        return ans;
    }
}
